package cn.com.qingz.modules.user.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

import cn.com.qingz.core.model.EasyUITree;
import cn.com.qingz.modules.system.service.impl.GeneralServiceImpl;
import cn.com.qingz.modules.user.mapper.CategoryMapper;
import cn.com.qingz.modules.user.model.Category;
import cn.com.qingz.modules.user.service.CategoryService;
import cn.com.qingz.modules.user.vo.CategorySearchCondition;

@Service
public class CategoryServiceImpl extends GeneralServiceImpl<Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    public void doAddOrUpdate(Category category) {
        category.setAddedTime("2017-03-03");
        if( category.getCategoryid() == null){
            categoryMapper.insert(category);
        }
        categoryMapper.updateByPrimaryKeySelective(category) ;
    }

    public PageList<Category> queryForPage(Object condition, int page, int limit) {
        return categoryMapper.queryForPage((CategorySearchCondition) condition, new PageBounds(page, limit));
    }

    public List<EasyUITree>  findChildrenByParent (EasyUITree parent) {
        List<EasyUITree> children =   categoryMapper.findChildrenByPid(parent.getId());
        if(children.isEmpty()) return new ArrayList<>();
        for (EasyUITree child : children) {
            findChildrenByParent(child);
        }
        parent.setChildren(children);
        return children;
    }

    public Category findByName(String name) {
        return categoryMapper.findByName(name);
    }

}
